#!/bin/sh

. /etc/utils.sh

if is-html-tv-ui; then
  echo "configured for chrome"
  exit 0
fi

start() {
  cd /app/sage
  (
    wait-until-created /tmp/time.synced
    if is-storage-box; then
      ./runsage &
    elif is-tv-box; then
      ./runsageclient &
    fi
  ) 2>&1 | logos s 4000000 50000000 &
  echo "Started sageserver $(cat /etc/version)"
}

stop() {
  pkillwait -f '(babysit.*)(runsage|chsrv|sage.Sage)'
  pkillwait -x 'runsage|chsrv|siege|java'
  stop_sagesrv
  stop_adsmgr
  stop_adscale
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    $0 stop; $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
